/**
 * Created by gs on 2017/1/17.
 */
var MySQLUtil = require('../config/mysql');
var Sequelize = require("sequelize");
var sequelize = MySQLUtil.sequelize();
var Menu = sequelize.define(
    's_menu',{
        id : {type : Sequelize.INTEGER, autoIncrement : true, primaryKey : true, unique : true},
        menu_name:{type : Sequelize.STRING},
        menu_pid:{type : Sequelize.INTEGER},
        menu_url:{type : Sequelize.STRING},
        menu_icon:{type : Sequelize.STRING},
        menu_class:{type : Sequelize.STRING},
        has_sub_menu:{type : Sequelize.INTEGER},
        status:{type : Sequelize.INTEGER},
        menu_type:{type : Sequelize.INTEGER},
        create_time:{type : Sequelize.DATE}
    },
    {
        tableName : 's_menu'
    }
);


exports.getAllList = function (params,callback) {
    //MySQLUtil.query('select * from s_menu', params, function (error,rows, fields) {
    //    console.log('rows', JSON.stringify(rows));
    //    callback(error,rows);
    //});
    Menu.findAll(params).then(function(data){
        callback(null,data);
    });
};

exports.getListPage = function (params,callback) {
    //params.sort = [{sortCol:'menu_pid',sortType:'desc'},{sortCol:'id',sortType:'desc'}];
    //MySQLUtil.query('select * from s_menu', params, function (error,rows, fields) {
    //    console.log('rows', JSON.stringify(rows));
    //    callback(error,rows);
    //});
    if(!params || !params.pageInfo.pageSize || !params.pageInfo.pageNum){
        params.pageInfo.pageSize = 10;
        params.pageInfo.pageNum = 1;
    }
    Menu.findAndCountAll({
        where : params.condition,
        order : [['id', 'DESC']],
        limit : parseInt(params.pageInfo.pageSize),
        offset : (params.pageInfo.pageNum - 1) * params.pageInfo.pageSize
    }).then(function(data){
        callback(null,data);
    });

};
exports.getListTotalCount = function (params,callback) {
    MySQLUtil.queryTotalCount('select * from s_menu', params, function (error,rows, fields) {
        console.log('rows', JSON.stringify(rows));
        callback(error,rows);
    });
};

exports.getListByParams = function (params,callback) {
    MySQLUtil.query('select * from s_menu where ?', params, function (error,rows, fields) {
        console.log('rows', JSON.stringify(rows));
        console.log('fields', JSON.stringify(fields));
        callback(error,rows);
    });
};

exports.getMenuById = function (id, callback) {
    //MySQLUtil.query('select * from s_menu where id=?',[id], function (rows, fields) {
    //    console.log('rows', JSON.stringify(rows));
    //    callback(rows.length>0 ? rows[0]:{});
    //});
    Menu.find({
        where:{
            id : id
        }
    }).then(function (result) {
        callback(null,result);
    })
};

exports.save = function (json, callback) {
    console.log('json----'+json);
    //MySQLUtil.query('INSERT INTO s_menu SET ?',json, function (error,rows, fields) {
    //    console.log('rows', JSON.stringify(rows));
    //    callback(error,rows);
    //});
    Menu.create(json).then(function (result) {
        console.log(result);
        callback(null,result);
    })
};
exports.update = function (json, callback) {
    console.log('json----'+json);
    //MySQLUtil.query('INSERT INTO s_menu SET ?',json, function (error,rows, fields) {
    //    console.log('rows', JSON.stringify(rows));
    //    callback(error,rows);
    //});
    Menu.update(json,{
        where:{
            id:parseInt(json.id)
        }
    }).then(function (result) {
        console.log(result);
        callback(null,result);
    })
};
exports.delete = function (id, callback) {
    //MySQLUtil.query('INSERT INTO s_menu SET ?',json, function (error,rows, fields) {
    //    console.log('rows', JSON.stringify(rows));
    //    callback(error,rows);
    //});
    Menu.destroy({
        where:{
            id:parseInt(id)
        }
    }).then(function (result) {
        callback(null,result);
    })
};